package com.app.controller;

import com.app.bean.Movies;
import com.app.bean.Ticket;
import com.app.core.ReturnVoCommon;
import com.app.service.MoviesService;
import com.app.util.StringUtilsEx;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 师晓峰
 * @version V1.0
 * @date 2023/9/20 18:34
 * @Description: 用户相关的操作
 */
@Controller
@RequestMapping("/movies")
public class MoviesController {

    @Autowired
    private MoviesService moviesService;

    /**
     * 电影列表，带翻页功能
     *
     * @param moviename 电影名称
     * @param page       当前第几页
     * @param rows       每页大小
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> list(String moviename, Integer page, Integer rows) {
        Map<String, Object> map = new HashMap<>();
        if (StringUtilsEx.isEmpty(page)) {
            page = 1;
        }
        if (StringUtilsEx.isEmpty(rows)) {
            rows = 5;
        }
        // 分页
        PageHelper.startPage(page, rows);
        List<Movies> moviesList = moviesService.list(moviename);
        map.put("rows", moviesList);
        PageInfo<Movies> pageInfo = new PageInfo<Movies>(moviesList);
        map.put("total", pageInfo.getTotal());
        return map;
    }

    /**
     * 增加/修改一个电影
     *
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public ReturnVoCommon add(Movies movies) {
        ReturnVoCommon vo = new ReturnVoCommon();
        try {
            if (StringUtilsEx.isEmpty(movies.getId())){
                moviesService.add(movies);
            }else {
                moviesService.update(movies);
            }
        } catch (Exception e) {
            vo.setCode(-1);
            vo.setErrMsg("操作失败！");
        }
        vo.setCode(0);
        return vo;
    }

    /**
     * 删除一个电影
     *
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ReturnVoCommon delete(String id) {
        ReturnVoCommon vo = new ReturnVoCommon();
        try {
            moviesService.delete(id);
        } catch (Exception e) {
            vo.setCode(-1);
            vo.setErrMsg("删除失败！");
        }
        vo.setCode(0);
        return vo;
    }

    /**
     * 查找一个电影
     *
     * @return
     */
    @RequestMapping("/getById")
    @ResponseBody
    public Movies getById(String id) {
        Movies movies = moviesService.getById(id);
        return movies;
    }

    /**
     * 属性设置
     *  是否 最受期待
     *  是否 好评如潮
     *  是否 今日票房
     * @param id
     * @param column
     */
    @RequestMapping("/update")
    @ResponseBody
    public ReturnVoCommon update(String id,String column){
        ReturnVoCommon vo = new ReturnVoCommon();
        try {
            moviesService.update(id,column);
        } catch (Exception e) {
            vo.setCode(-1);
            vo.setErrMsg("设置失败！");
        }
        vo.setCode(0);
        return vo;
    }

    /**
     * 创建某场电影的所有票
     * @param movieId
     * @return
     */
    @RequestMapping("/createTickets")
    @ResponseBody
    public ReturnVoCommon createTickets(String movieId){
        ReturnVoCommon vo = new ReturnVoCommon();
        // 先检查电影票是否已经生成
        List<Ticket> ticketList = moviesService.getTicketsByMovieId(movieId);
        if (ticketList.size() > 0){
            vo.setCode(2);
            return vo;
        }
        try {
            moviesService.createTickets(movieId);
        } catch (Exception e) {
            vo.setCode(-1);
            vo.setErrMsg("生成失败！");
            return vo;
        }
        vo.setCode(0);
        return vo;
    }

    /**
     * 创建一个订单
     * 根据电影票id 生成订单
     * @param id  电影票的id
     */
    @RequestMapping("/addOrder")
    @ResponseBody
    public ReturnVoCommon addOrder(String id){
        ReturnVoCommon vo = new ReturnVoCommon();
        vo.setCode(0);
        try{
            moviesService.addOrder(id);
        }catch (Exception e){
            vo.setCode(-1);
            vo.setErrMsg("生成失败");
            return vo;
        }
        return vo;
    }
}
